<?php

/**
 * comments actions.
 *
 * @package    master_directory
 * @subpackage comments
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class commentsActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeSaveComment(sfWebRequest $request)
  {

    $template_id = $request->getParameter("template_id");
    $categories_contents_ids = $request->getParameter("category_content_ids");
    $content_ids = explode('-', $categories_contents_ids);
    
    $comment = new Comment();
    //TODO: facebook data d los cojones
    $comment->setUsername($this->getRequestParameter("h_fb_user"));
    $comment->setEmail("yay@yay.com");
    $comment->setTemplateId($template_id);
    $comment->setDate(date("Y-m-d H:i:s"));
    $comment->save();
  	//echo 'hereherehereherehereherehere'; exit();
    
    foreach ($content_ids as $content_id) {
    	$comment2content = new Comment2categoryContent();
    	$comment2content->setCategoryContentId($content_id);
    	$comment2content->setCommentId($comment->getId());
    	$comment2content->save();
    }
    
    $template = Doctrine_Core::getTable('Template')->find($template_id);
    $comment_fields = $template->getCommentField();
    foreach ($comment_fields as $comment_field) {
    	$value = $request->getParameter("comment_field_id_" . $comment_field->getId());
    	$field_value = new CommentFieldValue();
    	$field_value->setValue($value);
    	$field_value->setCommentId($comment->getId());
    	$field_value->setCommentFieldId($comment_field->getId());
    	$field_value->save();
    }
    
    $selected_comments = array();
    $comments = $template->getComment();
    foreach ($comments as $acomment){
   		$comment_category_ids = array();
		$comments2catcontent = $acomment->getComment2categoryContent();
		foreach ($comments2catcontent as $comment2catcontent){
			array_push($comment_category_ids, $comment2catcontent->getCategoryContentId());	
		}

		$common_category_content_ids = array_intersect($content_ids, $comment_category_ids);
		
		if (count($common_category_content_ids) == count($content_ids) && count($content_ids) != 0){
			
				array_push($selected_comments, $acomment);	
			
		}    	
    }
    
    
  	    return $this->redirect($request->getParameter("my_current_url"));
    
    
   // return $this->renderPartial("comments/comments", array('comments' => $selected_comments, 'comment_fields' => $comment_fields, 'template_id' => $template_id, 'category_content_ids' => $content_ids));
  }
  
    public function executeTestFacebook(){
    	$this->setLayout('layout_test');
    }
    
    public function executeRate(sfWebRequest $request){
    	$comment_id = $request->getParameter("comment_id");
    	$rate = $request->getParameter("rating");
    	
    	$rating = new Rating();
    	$rating->setCommentId($comment_id);
    	$rating->setRate($rate);
    	$rating->setDate(date("Y-m-d"));
    	$rating->save();
    	
    	$rated_comments = $this->getUser()->getAttribute("comments") != null ? $this->getUser()->getAttribute("comments") : array();
    	
    	array_push($rated_comments, $comment_id);
    	
    	$rated_comments = array_unique($rated_comments);
    	
    	$this->getUser()->setAttribute("comments", $rated_comments);
    	
    	$comment = Doctrine_Core::getTable('Comment')->find($comment_id);
    	
    	$rate = $comment->getAverageRating();
    	
    	/*$comment_ratings = $comment->getRating();
    	
    	$rate = 0;
    	
    	foreach ($comment_ratings as $comment_rating){
    		$rate += $comment_rating->getRate();
    	}
    	
    	$rate = round($rate/count($comment_ratings), 1);*/
    	
    	return $this->renderPartial("comments/rate", array('comment_id' => $comment_id, 'rate' => $rate));
    	
    	/*$this->comment_id = $comment_id;
    	
    	$this->rate = $rate;*/
    	
    }
}
